<?php

class imageUpload {
    var $filename, $filewidth, $filehieght, $stringdim, $selectQ, $updateQ;
    var $existQ, $removeQ, $mode, $image_field, $title = '', $title_field, $cropToHeight, $action, $itemName;
    var $mess = '';
    var $thmbwidth = 0;
    var $valid = false;
    var $thmbheight = 0;
    var $toRender = Array();

    function imageUpload($mode, $id) {
        global $lang;
        if(!isset($_POST['itemID'])) {
            $_POST['itemID'] = -1;
        }
        if($mode == 'teams') {
            $this->filename = 'team';
            $this->filewidth = 70;
            $this->fileheight = 70;
            $this->thmbwidth = 30;
            $this->thmbheight = 30;
            $this->stringdim = '70x70px';
            $this->selectQ = "select * from ". TEAMS_TABLE . " Where t_id = " . $id;
            $this->existQ = "select * from ". TEAMS_TABLE . " Where t_id = " . $_POST['itemID'];
            $this->removeQ =  "update ". TEAMS_TABLE . " Set t_logo = '', t_logoThmb = '' Where t_id = " . $_POST['itemID'];
            $this->updateQ = "update ". TEAMS_TABLE . " Set t_logo = '{logourl}', t_logoThmb = '{thmburl}' Where t_id = " .
                $_POST['itemID'];
            $this->title = $lang['TeamLogo'];
            $this->title_field = 't_name';
            $this->image_field = 't_logo';
            $this->image_id = 't_id';
            $this->form_id = 't_id';
            $this->action = 'usercp.php?action=teamadmin';
            $this->mode = $mode;
            $this->cropToHeight = false;
            $this->itemName = 'itemID';
            $this->valid = true;
        }
        if($mode == 'champimage') {
            $this->filename = 'champentry';
            $this->filewidth = 640;
            $this->fileheight = 480;
            $this->thmbwidth = 100;
            $this->thmbheight = 75;
            $this->stringdim = '640x480px';
            $this->selectQ = "select cs_image, c_id, c_title, CONCAT(cs_champ, '_', cs_driver) AS logo_id from ". CHAMPSIGNUPS_TABLE . ", " .
                CHAMPIONSHIPS_TABLE . " Where cs_champ = c_id AND cs_entryOwner = " . $_SESSION['valid_id'] . " AND cs_champ = " . $id;
            $this->existQ = "select cs_image, c_id, c_title, CONCAT(cs_champ, '_', cs_driver) AS logo_id from ". CHAMPSIGNUPS_TABLE . ", " .
                CHAMPIONSHIPS_TABLE . " Where cs_champ = c_id AND cs_entryOwner = " . $_SESSION['valid_id'] . " AND cs_champ = " . $id;
            $this->removeQ =  "update ". CHAMPSIGNUPS_TABLE . " Set cs_image = '', cs_imageThmb = '' Where cs_entryOwner = " . $id;
            $this->updateQ = "update ". CHAMPSIGNUPS_TABLE . " Set cs_image = '{logourl}', cs_imageThmb = '{thmburl}' Where cs_entryOwner = " .
                $_SESSION['valid_id'] . " AND cs_champ = " . $id;
            $this->title = $lang['ChampSignupImage'];
            $this->title_field = 'c_title';
            $this->image_field = 'cs_image';
            $this->image_id = 'logo_id';
            $this->form_id = 'c_id';
            $this->itemName = 'champid';
            $this->action = 'usercp.php?action=mychampionships';
            $this->mode = $mode;
            $this->cropToHeight = true;
            $this->valid = true;

        }

        if($this->valid) {
            if(isset($_POST['uploadLogo']) && isset($_POST['itemID'])) {
                $this->doUpload();
            }
            else if(isset($_POST['removeLogo']) && isset($_POST['itemID'])) {
                    $this->doRemove();
                }
            $this->setupEdit();
        }
    }

    function render() {
        if($this->valid) {
            include_once('includes/addMessage.php');

            if($this->mess != '') {
                $this->mess->render();
            }

            foreach( $this->toRender as $key => $value) {
                $value->render();
            }
        }
    }

    function doRemove() {

        global $db, $lang;

        //get the record
        $result = $db->sql_query($this->existQ);
        if($row = $db->sql_fetchrow($result)) {
            $result = $db->sql_query($this->removeQ);
            $this->mess = new addMessage(str_replace('{entry}', $row[$this->title_field], $lang['AdminRemoved']), $lang['AdminRemovedTitle']);

        }
        else {
            $this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);

        }
    }

    function doUpload() {
        global $db, $lang;

        $result = $db->sql_query($this->existQ);
        if($row = $db->sql_fetchrow($result)) {
            $image = 0;
            if( ($_FILES['uploadedfile']['type']=="image/gif") ) {
                $image = imagecreatefromgif($_FILES['uploadedfile']['tmp_name']);
            }
            elseif($_FILES['uploadedfile']['type']=="image/jpeg" || $_FILES['uploadedfile']['type']=="image/pjpeg" ) {
                $image = imagecreatefromjpeg($_FILES['uploadedfile']['tmp_name']);
            }
            elseif( $_FILES['uploadedfile']['type']=="image/png") {
                $image = imagecreatefrompng($_FILES['uploadedfile']['tmp_name']);
            }
            else {
                $this->mess = new addMessage($lang['LogoError'], $lang['LogoErrorTitle']);
            }

            if($image != 0) {
            //find out how many screenshots we already have
                $screens = $this->filename . $row[$this->image_id];

                //target file name
                $imageurl = "images/" . $screens . ".jpg";

                //resize and crop our image
                $image = cropImage($this->filewidth, $this->fileheight, $image, $this->cropToHeight);

                if(imagejpeg($image,$imageurl,95) ) {
                    $valid = false;
                    $thmburl = '';
                    $thmbimage = '';
                    if($this->thmbwidth > 0) {
                        $thmbimage = cropImage($this->thmbwidth, $this->thmbheight, $image);
                        $thmburl = "images/" . $screens . "_thmb.jpg";
                        if(imagejpeg($thmbimage,$thmburl,90) ) {
                            $valid = true;
                        }
                    }
                    else {
                    //don't need a thmb
                        $valid = true;
                    }
                    //get the record
                    $result = $db->sql_query($this->existQ);
                    if($valid && $row = $db->sql_fetchrow($result)) {
                        $result = $db->sql_query(str_replace('{thmburl}', $thmburl,
                            str_replace('{logourl}', $imageurl,$this->updateQ)));
                        $this->mess = new addMessage(str_replace('{entry}', $row[$this->title_field],
                            $lang['AdminUpdated']), $lang['AdminUpdatedTitle']);
                    }
                    else {
                        $this->mess = new addMessage($lang['LogoError'], $lang['LogoErrorTitle']);
                    }
                }
                else {
                    $this->mess = new addMessage($lang['LogoError'], $lang['LogoErrorTitle']);
                }
            }
            else {
                $this->mess = new addMessage($lang['LogoError'], $lang['LogoErrorTitle']);
            }
        }
        else {
            $this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
        }
    }

    function setupEdit() {
        global $db, $lang;
        include_once('includes/addMessage.php');
        include_once('includes/editLogo.php');

        //get the record

        $result = $db->sql_query($this->selectQ);
        while($row = $db->sql_fetchrow($result)) {
            $title = $this->title == '' ? $row[$this->title_field] : $this->title;
            $this->toRender[] = new editLogo($this->action, $row[$this->form_id], $title, $row[$this->image_field], $this->stringdim, $this->itemName);
        }
    }
}
?>